package com.edgclub.edg.receiver;

import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.edgclub.edg.Constant;
import com.edgclub.edg.activity.GzMainActivity;
import com.edgclub.edg.model.Topic;
import com.xiaomi.mipush.sdk.ErrorCode;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.xiaomi.mipush.sdk.MiPushCommandMessage;
import com.xiaomi.mipush.sdk.MiPushMessage;
import com.xiaomi.mipush.sdk.PushMessageReceiver;

import java.util.HashMap;
import java.util.List;

/**
 * 1、PushMessageReceiver是个抽象类，该类继承了BroadcastReceiver。
 * 2、需要将自定义的DemoMessageReceiver注册在AndroidManifest.xml文件中 <receiver
 * android:exported="true"
 * android:name="com.xiaomi.mipushdemo.DemoMessageReceiver"> <intent-filter>
 * <action android:name="com.xiaomi.mipush.RECEIVE_MESSAGE" /> </intent-filter>
 * <intent-filter> <action android:name="com.xiaomi.mipush.ERROR" />
 * </intent-filter> </receiver>
 * 3、DemoMessageReceiver的onCommandResult方法用来接收客户端向服务器发送命令后的响应结果
 * 4、DemoMessageReceiver的onReceiveMessage方法用来接收服务器向客户端发送的消息
 * 5、onReceiveMessage和onCommandResult方法运行在非UI线程中
 *
 * @author wangkuiwei
 */
public class XiaoMiMessageReceiver extends PushMessageReceiver {

	public static final String TAG = "xiaomi";
	public static final int SYSMSG = 0;
	public static final int NOTIFYMSG = 5;

	private String mRegId;
	private long mResultCode = -1;
	private String mReason;
	private String mCommand;
	private String mMessage;
	private String mTopic;
	private String mAlias;
	private String mAccount;
	private String mStartTime;
	private String mEndTime;

	@Override
	public void onReceivePassThroughMessage(Context context, MiPushMessage message){
		Log.v(TAG,
				"onReceivePassThroughMessage is called. " + message.toString());
		if(!TextUtils.isEmpty(message.getTopic())) {
			mTopic=message.getTopic();
		} else if(!TextUtils.isEmpty(message.getAlias())) {
			mAlias=message.getAlias();
		}
		HashMap<String ,String> extraSString=new HashMap<>();
		try {
			extraSString= (HashMap<String, String>) message.getExtra();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (message.isNotified()) {
			if(null!=extraSString){
				String protocol=extraSString.get("protocol");
				if(null!=protocol&&protocol.contains("gz://news")){
					String id=protocol.substring(protocol.lastIndexOf("/")+1);
					Intent i = new Intent(context,GzMainActivity.class);
					i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
					Topic mTopic=new Topic();
//					mTopic.setUrl(ApiConfig.NewH5+id);
//					mTopic.setId(id);
//					mTopic.setTitle(message.getTitle());
//					mTopic.setAbstractStr(message.getContent());
//					mTopic.setTopicId(id);
//					i.putExtra("url", ApiConfig.NewH5+id);
//					i.putExtra("obj", mTopic);
					NotificationClient.create(context).show(
							message.getTitle(),
							message.getContent(), i);
				}
			}
		}


	}

	@Override
	public void onNotificationMessageClicked(Context context, MiPushMessage message){
		Log.v(TAG,
				"onNotificationMessageClicked is called. " + message.toString());

		if(!TextUtils.isEmpty(message.getTopic())) {
			mTopic=message.getTopic();
		} else if(!TextUtils.isEmpty(message.getAlias())) {
			mAlias=message.getAlias();
		}
		HashMap<String ,String> extraSString=new HashMap<>();
		try {
			extraSString= (HashMap<String, String>) message.getExtra();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (message.isNotified()) {
			if(null!=extraSString){
				String extra = message.getExtra().get("action");
				if ("posts_reply".equals(extra)){
					Intent i = new Intent(context,GzMainActivity.class);
					i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					i.putExtra("MiPushu_Type", Constant.MiPush_Type_Reply);
					context.startActivity(i);
				} else if ("report_msg".equals(extra)){
					Intent i = new Intent(context,GzMainActivity.class);
					i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					i.putExtra("MiPushu_Type", Constant.MiPush_Type_Report);
					context.startActivity(i);
				}else if("posts_msg".equals(extra)){
					Intent intent = new Intent(context, GzMainActivity.class);
					String postId = message.getExtra().get("postsId");
					intent.putExtra("postId",postId);
					intent.putExtra("MiPushu_Type", Constant.MiPush_Type_Focus);
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					context.startActivity(intent);
				}else{
						Intent i = new Intent(context,GzMainActivity.class);
						i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						i.putExtra("MiPushu_Type", Constant.MiPush_Type_Other);
						context.startActivity(i);
				}
			}

		}
	}

	@Override
	public void onNotificationMessageArrived(Context context, MiPushMessage message){
		Log.v(TAG,
				"onNotificationMessageArrived is called. " + message.toString());

		if(!TextUtils.isEmpty(message.getTopic())) {
			mTopic=message.getTopic();
		} else if(!TextUtils.isEmpty(message.getAlias())) {
			mAlias=message.getAlias();
		}

		Message msg = Message.obtain();
	}

	@Override
	public void onCommandResult(Context context, MiPushCommandMessage message) {
		Log.v(TAG,
				"onCommandResult is called. " + message.toString());
		String command = message.getCommand();
		List<String> arguments = message.getCommandArguments();
		String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
		String cmdArg2 = ((arguments != null && arguments.size() > 1) ? arguments.get(1) : null);
		String log = "";
		if (MiPushClient.COMMAND_REGISTER.equals(command)) {
			if (message.getResultCode() == ErrorCode.SUCCESS) {
				mRegId = cmdArg1;
			} else {
			}
		} else if (MiPushClient.COMMAND_SET_ALIAS.equals(command)) {
			if (message.getResultCode() == ErrorCode.SUCCESS) {
				mAlias = cmdArg1;
			} else {
			}
		} else if (MiPushClient.COMMAND_UNSET_ALIAS.equals(command)) {
			if (message.getResultCode() == ErrorCode.SUCCESS) {
				mAlias = cmdArg1;
			} else {
			}
		}  else if (MiPushClient.COMMAND_SET_ACCOUNT.equals(command)) {
			if (message.getResultCode() == ErrorCode.SUCCESS) {
				mAccount = cmdArg1;
			} else {
			}
		} else if (MiPushClient.COMMAND_UNSET_ACCOUNT.equals(command)) {
			if (message.getResultCode() == ErrorCode.SUCCESS) {
				mAccount = cmdArg1;
			} else {
			}
		} else if (MiPushClient.COMMAND_SUBSCRIBE_TOPIC.equals(command)) {
			if (message.getResultCode() == ErrorCode.SUCCESS) {
				mTopic = cmdArg1;
			} else {
			}
		} else if (MiPushClient.COMMAND_UNSUBSCRIBE_TOPIC.equals(command)) {
			if (message.getResultCode() == ErrorCode.SUCCESS) {
			} else {
			}
		} else if (MiPushClient.COMMAND_SET_ACCEPT_TIME.equals(command)) {
			if (message.getResultCode() == ErrorCode.SUCCESS) {
				mStartTime = cmdArg1;
				mEndTime = cmdArg2;
			} else {
			}
		} else {
			log = message.getReason();
		}

		Message msg = Message.obtain();
		msg.obj = log;
	}

	@Override
	public void onReceiveRegisterResult(Context context, MiPushCommandMessage message){
		Log.v(TAG,
				"onReceiveRegisterResult is called. " + message.toString());
		String command = message.getCommand();
		List<String> arguments = message.getCommandArguments();
		String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
		String log;
		if (MiPushClient.COMMAND_REGISTER.equals(command)) {
			if (message.getResultCode() == ErrorCode.SUCCESS) {
				mRegId = cmdArg1;
			} else {
			}
		} else {
			log = message.getReason();
		}

		Message msg = Message.obtain();
	}


}

